//
//  MainTabbar.swift
//  SinaWeibo
//
//  Created by 王佳琳 on 16/2/16.
//  Copyright © 2016年 王佳琳. All rights reserved.
//

import UIKit

class MainTabbar: UITabBar {

    override func layoutSubviews() {
        super.layoutSubviews()
        //给tabbar设置尺寸
        let w = self.bounds.width/5
        let h = self.bounds.height
        let rect = CGRect(x: 0, y: 0, width: w, height: h)
        
        //定义一个变量  记录递增的索引
        var index: CGFloat = 0
        
        //遍历子视图  获取UITabbar
        for subView in subviews {
            if subView.isKindOfClass(NSClassFromString("UITabBarButton")!){
                print(subView)
                subView.frame = CGRectOffset(rect, w * index, 0)
                index += (index == 1 ? 2 :1)
            }
        }
        //设置加号的按钮位置
        plusBtn.frame = CGRectOffset(rect, 2 * w, 0)
    }
    //懒加载加号按钮
    lazy var plusBtn: UIButton = {
        let btn = UIButton()
        //设置背景图片
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
        //设置图片
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        
        self.addSubview(btn)
        
        return btn
    }()
}
